package com.hbx.controller;


import com.hbx.aspect.annotation.SensitiveFilter;
import com.hbx.dto.comment.InsertCommentDto;
import com.hbx.service.CommentService;
import com.hbx.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hbx
 * @since 2022-01-15
 */
@RestController
@RequestMapping("/comment")
@Api(tags = "评论处理器")
public class CommentController {
    @Autowired
    private CommentService commentService;

    /**
     * 添加评论
     * @param dto
     * @param result
     * @return
     */
    @RequestMapping(value = "/addComment",method = RequestMethod.POST)
    @ApiOperation("添加评论处理器方法")
    @SensitiveFilter
    public Result addComment(@RequestBody @Valid InsertCommentDto dto, BindingResult result){
        Result r = new Result();
        if (result.hasErrors()){
            r.setCode(400).setMsg(result.getFieldError().getDefaultMessage());
            return r;
        }
        commentService.insertComment(dto, r);
        return r;
    }

    /**
     * 删除评论
     * @param commentId
     * @return
     */
    @RequestMapping(value = "/removeComment",method = RequestMethod.GET)
    @ApiOperation("删除评论处理器")
    public Result removeComment(@RequestParam("commentId") Integer commentId){
        Result r = new Result();
        if (commentId == null){
            r.setCode(400).setMsg("入参错误,commentId为必传项");
            return r;
        }
        commentService.removeComment(commentId, r);
        return r;
    }
}

